package com.bowlong.util;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

@SuppressWarnings({ "rawtypes" })
public class LRUCache<K, V> extends LinkedHashMap<K, V> {

	public static <K, V> LRUCache<K, V> create(int maxSize) {
		return new LRUCache<K, V>(maxSize);
	}

	public static <K, V> Map<K, V> createSafe(int maxSize) {
		return Collections.synchronizedMap(new LRUCache<K, V>(maxSize));
	}

	private LRUCache(int maxSize) {
		super(maxSize, 0.75F, true);
		maxElements = maxSize;
	}

	protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
		return size() > maxElements;
	}

	private static final long serialVersionUID = 1L;
	protected int maxElements;

}